package lesson.LockTest;

import java.util.concurrent.locks.ReentrantLock;

public class MyThread8 implements Runnable{
    private int ticket = 100;
    private Object obj = new Object();
    private ReentrantLock lock = new ReentrantLock();

    @Override
    public void run() {
        while (true){

            try {
                lock.lock();
                if(ticket <= 0){
                    break;
                }else {
                    ticket--;
                    System.out.println(Thread.currentThread().getName()+"在卖票，还剩"+ticket+"张票");
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }finally {
                lock.unlock();
            }

        }
    }
}
